package nz.ac.massey.doi_information_extractor.output;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import nz.ac.massey.doi_information_extractor.data.Author;
import nz.ac.massey.doi_information_extractor.data.AuthorList;
import nz.ac.massey.doi_information_extractor.data.Publication;

public class PDFOutput implements PDFOutputInterface {

	@Override
	public void outputToPdf(Publication publication, String path) throws OutputException {
		try {

			// Create document at the defined path
			Document document = new Document();
			PdfWriter.getInstance(document, new FileOutputStream(path));
			document.open();
			document.add(new Paragraph("Academic Output Coversheet"));
			document.add(new Paragraph(" "));

			// Add the ID table
			PdfPTable idTable = new PdfPTable(1);
			idTable.addCell("ID NUMBER: " + publication.getPublicationId());
			document.add(idTable);
			document.add(new Paragraph(" "));

			// Add the main contributor table
			PdfPTable mainContributorTable = new PdfPTable(2);
			int contributorIndex = publication.getAuthors().getStaffSubmitter();


			Author submitter = null;

			if(contributorIndex > 0)
				submitter = (Author) publication.getAuthors().get(contributorIndex);
			else
				submitter = new Author();

			mainContributorTable.addCell("Name:");
			mainContributorTable.addCell(submitter.getAuthorFirstName() + " " + submitter.getAuthorLastName());

			mainContributorTable.addCell("Department:");
			mainContributorTable.addCell(submitter.getDepartment());

			mainContributorTable.addCell("Organisation:");
			mainContributorTable.addCell(submitter.getOrganisation());

			document.add(mainContributorTable);
			document.add(new Paragraph(" "));


			// Add the publication information based on the publication type
			String publicationType = publication.getPublicationType();

			if(publicationType.equals("Journal")){
				printJournal(document, publication);
			}else if(publicationType.equals("Book")){
				printBook(document, publication);
			}else if(publicationType.equals("Conference")){
				printConference(document, publication);
			}

			PdfPTable doiTable = new PdfPTable(2);
			doiTable.addCell("DOI:");
			doiTable.addCell(publication.getDoi());

			document.add(doiTable);
			document.add(new Paragraph(" "));

			// Add booleans table
			PdfPTable booleansTable = new PdfPTable(2);
			booleansTable.addCell("Research or Professional/Community?");
			if (publication.getResearchOrProfCom()){
				booleansTable.addCell("Research");
			}else{
				booleansTable.addCell("Professional/Community");
			}

			booleansTable.addCell("Quality Assured?");
			if (publication.getQualityAssured()){
				booleansTable.addCell("Yes");
			}else{
				booleansTable.addCell("No");
			}

			booleansTable.addCell("Confidential?");

			if (publication.getConfidential()){
				booleansTable.addCell("Yes");
			}else{
				booleansTable.addCell("No");
			}

			document.add(booleansTable);
			document.add(new Paragraph(" "));

			// Date received table
			PdfPTable dateReceivedTable = new PdfPTable(2);
			dateReceivedTable.addCell("Date Received (office use only)");
			dateReceivedTable.addCell(" ");

			document.add(dateReceivedTable);
			document.close();
		} catch (DocumentException documentException) {
			throw new OutputException("Error formatting PDF document");
		}catch(IOException ioException){
			throw new OutputException("Error creating PDF file");
		}catch(NullPointerException nullException){
			throw new OutputException("Publication not properly formatted");
		}
	}

	/*
	 * Journal Data
	Journal Title*
	Article Title*
	URL*
	Edition*
	Year*
	Volume*
	Pages*
	ISSN/ISBN*
	Number (journal)*/
	private void printJournal(Document document, Publication publication) throws DocumentException{
		PdfPTable table3 = new PdfPTable(2);
		PdfPCell cell = new PdfPCell(new Paragraph("Journal"));
		cell.setColspan(2);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		table3.addCell(cell);

		table3.addCell("Author(s)");

		AuthorList authors = publication.getAuthors();
		String authorsString = "";

		for (int i = 0; i < authors.size(); i++) {
			Author currAuthor = authors.get(i);
			if (i == authors.size() - 1)
				authorsString += currAuthor.getAuthorFirstName()
						+  " " + currAuthor.getAuthorLastName() + ".";
			else
				authorsString += currAuthor.getAuthorFirstName()
						+ " " + currAuthor.getAuthorLastName() + ", ";
		}

		table3.addCell(authorsString);

		table3.addCell("Journal / Publication Title");
		table3.addCell(publication.getPublicationTitle());

		table3.addCell("Article title");
		table3.addCell(publication.getArticleTitle());

		table3.addCell("URL");
		table3.addCell(publication.getUrl());

		table3.addCell("Edition");
		table3.addCell(publication.getEdition());

		table3.addCell("Publication Date");
		table3.addCell(publication.getDate());

		table3.addCell("ISSN");
		table3.addCell(publication.getIssn());

		table3.addCell("Volume / number");
		table3.addCell(publication.getVolumeNumber());

		table3.addCell("Page Numbers");
		table3.addCell(publication.getPageNumbers());

		document.add(table3);
		document.add(new Paragraph(" "));
	}

	/*
	 * Book Data
	Publication Title*
	Publisher*
	Place Published*
	URL*
	Edition*
	Year*
	Pages*
	ISSN/ISBN*
	*/
	private void printBook(Document document, Publication publication) throws DocumentException{
		PdfPTable table3 = new PdfPTable(2);
		PdfPCell cell = new PdfPCell(new Paragraph("Book"));
		cell.setColspan(2);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		table3.addCell(cell);

		table3.addCell("Author(s)");

		AuthorList authors = publication.getAuthors();
		String authorsString = "";

		for (int i = 0; i < authors.size(); i++) {
			Author currAuthor = authors.get(i);
			if (i == authors.size() - 1)
				authorsString += currAuthor.getAuthorFirstName()
						+ currAuthor.getAuthorLastName();
			else
				authorsString += currAuthor.getAuthorFirstName()
						+ currAuthor.getAuthorLastName() + ", ";
		}

		table3.addCell(authorsString);

		table3.addCell("Book Title");
		table3.addCell(publication.getPublicationTitle());

		table3.addCell("Publisher");
		table3.addCell(publication.getPublisher());

		table3.addCell("Place Published");
		table3.addCell(publication.getPublisherLocation());

		table3.addCell("URL");
		table3.addCell(publication.getUrl());

		table3.addCell("Edition");
		table3.addCell(publication.getEdition());

		table3.addCell("Publication Date");
		table3.addCell(publication.getDate());

		table3.addCell("ISSN");
		table3.addCell(publication.getIssn());

		table3.addCell("Page Numbers");
		table3.addCell(publication.getPageNumbers());

		document.add(table3);
		document.add(new Paragraph(" "));

	}

	/*
	 * Conference Data
	Paper Title*
	Proceedings Title*
	Location*
	Conference dates*
	URL*
	Editors*
	Edition*
	Year*
	Pages*
	ISSN/ISBN*
	 */
	private void printConference(Document document, Publication publication) throws DocumentException{
		PdfPTable table3 = new PdfPTable(2);
		PdfPCell cell = new PdfPCell(new Paragraph("Conference"));
		cell.setColspan(2);
		cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
		table3.addCell(cell);

		table3.addCell("Author(s)");

		AuthorList authors = publication.getAuthors();
		String authorsString = "";

		for (int i = 0; i < authors.size(); i++) {
			Author currAuthor = authors.get(i);
			if (i == authors.size() - 1)
				authorsString += currAuthor.getAuthorFirstName()
						+ currAuthor.getAuthorLastName();
			else
				authorsString += currAuthor.getAuthorFirstName()
						+ currAuthor.getAuthorLastName() + ", ";
		}

		table3.addCell(authorsString);

		table3.addCell("Paper Title");
		table3.addCell(publication.getPublicationTitle());

		table3.addCell("Proceedings Title");
		table3.addCell(publication.getArticleTitle());

		table3.addCell("Location");
		table3.addCell(publication.getConferenceLocation());

		table3.addCell("Conference Date");
		table3.addCell(publication.getConferenceDates());

		table3.addCell("URL");
		table3.addCell(publication.getUrl());

		table3.addCell("Editors");
		table3.addCell(publication.getEditors());

		table3.addCell("Edition");
		table3.addCell(publication.getEdition());

		table3.addCell("Year");
		table3.addCell(publication.getDate());

		table3.addCell("Page Numbers");
		table3.addCell(publication.getPageNumbers());

		table3.addCell("ISSN");
		table3.addCell(publication.getIssn());

		document.add(table3);
		document.add(new Paragraph(" "));
	}
}
